package com.tzu.gateway.bean.handler;

import com.tzu.gateway.bean.OrderCmdContainer;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import thirdpart.bean.CommonMsg;
import thirdpart.codec.IBodyCodec;
import thirdpart.order.OrderCmd;


/**
 * @Author: Tzu
 * @Date: 2020/11/15 16:23
 * @Version: 1.0
 * @Description:
 */

@Log4j2
@AllArgsConstructor
public class MsgHandler implements IMsgHandler {

    private IBodyCodec bodyCodec;

    @Override
    public void onCounterData(CommonMsg msg) {
        OrderCmd orderCmd;

        try {
            //把msg对象里的body属性信息反序列化成orderCmd
            orderCmd = bodyCodec.deserialize(msg.getBody(), OrderCmd.class);
            log.info("recv cmd:{}",orderCmd);

            //缓存orderCmd到内存（自定义的缓存队列）中
            if (!OrderCmdContainer.getInstance().cache(orderCmd)){
                //缓存失败：输出日志
                log.error("gateway queue insert fail,queue length:{},order:{}",
                        OrderCmdContainer.getInstance().size(),
                        orderCmd);
            }
        }catch (Exception e){
            log.error("decode order cmd error",e);
        }
    }
}
